﻿<?php
include_once('header.inc.php');
header('Content-Type:text/Calendar; charset=utf-8');
?>
<?php
$t = $db->select('*',$CONFIG['table_users'],'login=\''.$_GET['login'].'\' AND pass=\''.$_GET['pass'].'\'');
$n = count($t);
if($n!=1)
   exit('Erreur : login/mot de passe incorrect');
$br = "\r\n";
echo 'BEGIN:VCALENDAR'.$br;
echo 'PRODID:-//mOut powered//Anniversaires '.$CONFIG['version'].'//EN'.$br;
echo 'VERSION:2.0'.$br;
echo 'CALSCALE:GREGORIAN'.$br;
echo 'METHOD:PUBLISH'.$br;
echo 'X-WR-CALNAME:Anniversaires'.$br;
echo 'X-WR-TIMEZONE:Europe/Paris'.$br;
echo 'X-WR-CALDESC:Anniversaires'.$br;
echo 'X-WR-RELCALID:""'.$br;
echo 'BEGIN:VTIMEZONE'.$br;
echo 'TZID:Europe/Paris'.$br;
echo 'X-LIC-LOCATION:Europe/Paris'.$br;
echo 'BEGIN:DAYLIGHT'.$br;
echo 'TZOFFSETFROM:+0100'.$br;
echo 'TZOFFSETTO:+0200'.$br;
echo 'TZNAME:CEST'.$br;
echo 'DTSTART:19700329T020000'.$br;
echo 'RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU'.$br;
echo 'END:DAYLIGHT'.$br;
echo 'BEGIN:STANDARD'.$br;
echo 'TZOFFSETFROM:+0200'.$br;
echo 'TZOFFSETTO:+0100'.$br;
echo 'TZNAME:CET'.$br;
echo 'DTSTART:19701025T030000'.$br;
echo 'RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU'.$br;
echo 'END:STANDARD'.$br;
echo 'END:VTIMEZONE'.$br;

$tab = $db->select("
id,
firstname,
lastname,
birthdate,
mail,
phone,
DATE_FORMAT(birthdate, '%d') AS jj,
DATE_FORMAT(birthdate, '%m') AS mm,
DATE_FORMAT(birthdate, '%Y') AS yyyy,
(YEAR(CURRENT_DATE) - YEAR(birthdate)) - (RIGHT(CURRENT_DATE,5) <= RIGHT(birthdate,5)) +1 AS age,
TO_DAYS(CONCAT(LEFT(CURRENT_DATE,5), RIGHT(birthdate, 5))) - TO_DAYS(CURRENT_DATE) AS dans",
$CONFIG['table_birthdays'],
"userlogin='".$_GET['login']."' AND birthdate IS NOT NULL",
"dans"
);

function affLigneIcal($ligne){
  global $CONFIG, $br;
  echo "BEGIN:VEVENT".$br;
  $thisyear = $ligne['mm']>=date('m') || ($ligne['mm']==date('m') && $ligne['jj']>=date('d'));
  $annee = $thisyear ? date('Y') : date('Y')+1;
  $mois = $ligne['mm'];
  $jour = $ligne['jj'];
  echo 'DTSTART;VALUE=DATE:'.$annee.$mois.$jour.$br;
  if($jour=='31'
   || ($jour=='30' && ($mois=='04' || $mois=='06' || $mois=='09' || $mois=='11'))
   || ($mois=='02' && ($jour=='29') || (!date('L') && $jour=='28'))){
    $jour = '01';
    if($mois=='12'){
      $mois = '01';
      $annee++;
    }
    else{
      $mois = ( (int) $mois ) + 1;
      if($mois<10)
        $mois = '0'.$mois;
    }
  }
  else{
    $jour = ( (int) $jour ) + 1;
    if($jour<10)
        $jour = '0'.$jour;
  }
  echo 'DTEND;VALUE=DATE:'.$annee.$mois.$jour.$br;
  $ctime = date('Ymd').'T'.date('His').'Z';
  echo 'RRULE:FREQ=YEARLY;WKST=MO'.$br;
  echo 'DTSTAMP:'.$ctime.$br;
  echo 'ORGANIZER;CN=Anniversaires:MAILTO:'.$CONFIG['mail'].$br;
  echo 'UID:'.$_GET['login'].'_'.$ligne['id'].'@anniv.mout.fr'.$br;
  echo 'CLASS:PUBLIC'.$br;
  echo 'CREATED:'.$ctime.$br;
  echo 'LAST-MODIFIED:'.$ctime.$br;
  echo 'DESCRIPTION:';
    if($ligne['mail']!='')
      echo $ligne['mail'];
    if($ligne['mail']!='' && $ligne['phone']!='')
      echo '\n';
    if($ligne['phone']!='')
      echo $ligne['phone'];
    echo $br;
  echo 'LOCATION:'.$br;
  echo 'SEQUENCE:0'.$br;
  echo 'STATUS:CONFIRMED'.$br;
  echo 'TRANSP:TRANSPARENT'.$br;
  //echo 'SUMMARY:'.utf8_encode($ligne['firstname']).' '.utf8_encode($ligne['lastname']).' ('.$ligne['age'].' ans)'.$br;
  echo 'SUMMARY:'.utf8_encode($ligne['firstname']).' '.utf8_encode($ligne['lastname']).' ('.$ligne['yyyy'].')'.$br;
  echo 'END:VEVENT'.$br;
}
lister($tab,'ical');
echo 'END:VCALENDAR'.$br;
?>
